package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Professor;
import com.han.dao.UserDAO;

/**
 * Servlet implementation class ProfessorChangeProfileServlet
 */
@WebServlet("/Professor/ProfessorChangeProfileServlet")
public class ProfessorChangeProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(ProfessorChangeProfileServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfessorChangeProfileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Professor professor = null;
		UserDAO userdao = new UserDAO();
		
		String professorFirstName = null,professorLastName = null;
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor)session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("professorFirstName")!=null){
			professorFirstName = request.getParameter("professorFirstName");
		}
		else{
			
		}
		
		if(request.getParameter("professorLastName")!=null){
			professorLastName = request.getParameter("professorLastName");
		}
		else{
			
		}
		
		if(professor!=null){
			
			int userID = Integer.parseInt(professor.getUserID().substring(1));
			
			professor.setUsername(professorFirstName.trim().charAt(0)+professorLastName.replace(" ","")+userID);
			professor.setFirstName(professorFirstName);
			professor.setLastName(professorLastName);
			
			
			try {
				userdao.updateUser(professor);
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			response.sendRedirect("Professor/Profile.jsp");
		}
		
	}

}
